<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/6
 * Time: 23:06
 */

namespace Admin\Controller;

use Common\Controller\AdminController;
use Think\Page;

class ActInfoController extends AdminController
{
    /*
     * 显示活动（文章）
     * */
    public function index()
    {
        // 访问这个文章列表有权限设置  $_SESSION['admin']['admin_id'];
        $keyword = I('get.keyword','','htmlspecialchars');
        $search = I('get.search');
        $Act = D('ActInfo');
        if (!empty($keyword)) {
            $where['act_tittle|act_organizer']  = array('like', "%$keyword%");
        }

        if (!empty($search)) {
            $where['act_type_code']  = array('eq',$search);
        }

        $dataCount = $Act->where($where)->count();

        $page = new Page($dataCount, 10);
        $show = $page->show();

        $data = $Act->where($where)->limit($page->firstRow, $page->listRows)->select();

        $this->assign('data',$data);
        $this->assign('show',$show);

        layout(true);
        $this->display('article');
    }

    /*
     * 添加文章
     * */
    public function addArt()
    {
        $ActInfo = D('ActInfo'); //D参数：模型名 默认当前模块下的Model
        if (IS_POST) {
            $array=I('post.');
            $array['creat_time']=date('Y-m-d H:i:s');
            if ($ActInfo->create() && $ActInfo->add($array)) {
                //添加成功
                $this->success('添加成功', '/ActInfo/index');
                return;
            } else {
                // 自动验证没有通过 输出错误提示信息
                $errData = $ActInfo->getError();
                $this->assign('errData',$errData);
            }
        }
        layout(true);
        $this->display('add_article');
    }

    /*
     * 修改文章
     * */
    public function modArt($act_id)
    {
        $act_id = intval($act_id);
        $ActInfo =D('ActInfo');
        $data = $ActInfo->where(array(
            'act_id' => $act_id
        ))->find();

        if (!$data) {
            $this->redirect('/ActInfo/index');
        }

        if (IS_POST) {
            if ($ActInfo->create() && $ActInfo->save()) {
                $this->success('修改成功', '/ActInfo/index');
                return;
            } else {
                // 自动验证没有通过 输出错误提示信息
                $errData = $ActInfo->getError();
                $this->assign('errData',$errData);
            }
        }
        $this->assign('data',$data);
        layout(true);
        $this->display('mod_article');
    }

    /*
     * 删除文章
     * */
    public function delArt($act_id)
    {
        $act_id = intval($act_id);

        $ActInfo = M('ActInfo');
        $findData = $ActInfo->where(array(
            'act_id' => $act_id
        ))->find();

        if ($findData) {
            $ActInfo->delete($act_id);
            $this->success('删除成功', '/ActInfo/index');
        } else {
            $this->error('删除失败', '/ActInfo/index');
        }
    }

}
